//
//  AppDelegate.m
//  iFeller
//
//  Created by Wii on 15/12/10.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "AppDelegate.h"
@interface AppDelegate ()
@property (nonatomic , strong) XYSqlite3 *sql;
@end
@implementation AppDelegate

#pragma mark - getter
- (XYSqlite3 *)sql{
    if (!_sql) {
        _sql = [[XYSqlite3 alloc]init];
        [_sql openDataBase];
    }
    return _sql;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 建立数据表
    [self.sql creatTheTable:TableName_MyTask];
    [self.sql creatTheTable:TableName_TheTaskAtHand];
    [self.sql creatTheTable:TableName_FinishTask];
    
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"tags"];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSMutableArray *array = [self.sql searchTheDataBaseWithtableName:TableName_TheTaskAtHand];
    for (int i = 0; i < array.count; i++) {
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss +zzz"];
        NSDate *dateString = [formatter dateFromString:array[i][6]];
        if ([date timeIntervalSinceDate:dateString] >= 0) {
            [self.sql deleteTheDataBaseWithTableName:TableName_TheTaskAtHand deleteName:array[i][0]];
            [self.sql insertDataSourceToDBwithArray:array[i] tableName:TableName_MyTask];
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"backToForeGround" object:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"noti:%@",notification);
    [self.sql openDataBase];

    NSString *notMess = notification.userInfo[notification.userInfo[@"key"]];
    NSMutableArray *array = [NSMutableArray array];
    array = [self.sql conditionSearchTheDataBaseWithTableName:TableName_TheTaskAtHand searchName:notMess];
    
    [self.sql insertDataSourceToDBwithArray:array[0] tableName:TableName_MyTask];
    [self.sql deleteTheDataBaseWithTableName:TableName_TheTaskAtHand deleteName:notMess];
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"待办事项" message:notMess preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"front" object:nil];
    }]; 

    [alertVC addAction:confirmAC];
    [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
}

@end
